using System;
using System.Collections.Generic;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Graphics;
using Sce.PlayStation.Core.Imaging;

using Sample;

namespace Nyanimals
{
    public class CTextRender
    {
        public class TextData
        {
            private int posX;
            private int posY;
            private Color col;
            private string txt;
            
            private CTextRender Owner;
            
            public TextData(string text, int X, int Y, Color c, CTextRender o)
            {
                txt = text;
                posX = X;
                posY = Y;
                col = c;
                Owner = o;
            }
            
            private void D()
            {
                Owner.Dirty = true;
            }
            
            public int PositionX
            {
                get {return posX;}
                set { posX = value; D();}
            }
            public int PositionY
            {    
                get {return posY;}
                set { posY = value; D();}
            }
            public Color ARGB
            {    
                get {return col;}
                set { col = value; D();}
            }
            public string Text
            {    
                get {return txt;}
                set { txt = value; D();} 
            }
        }
        public struct Color
        {
            public int R, G, B, A;
            public Color(int r, int g, int b, int a)
            {
                R = r;
                G = g;
                B = b;
                A = a;
            }
        };
        
        int width;
        int height;   
        bool dirty;
        
        Image img;
        List<TextData> texts;
        Texture2D texture;
        FrameBuffer frame;
        
        public bool Dirty
        {
            get { return dirty;}
            set { dirty = value;}
        }
        
        public Texture2D RenderTexture
        {
            get { return texture;}
        }
        
        public CTextRender(int textureWidth, int textureHeight)
        {
            width = textureWidth;
            height = textureHeight;
           
            
            texts = new List<TextData>();
            texture = new Texture2D(width, height, false, PixelFormat.Rgba, PixelBufferOption.Renderable);
            frame = new FrameBuffer();
            
            frame.SetColorTarget(texture, 0);
            frame.SetDepthTarget(null);
            
            img = new Image(ImageMode.Rgba, new ImageSize(width, height), new ImageColor(0, 0, 0, 0));
            
            dirty = true;
        }
        
        public void RemoveText(TextData data)
        {
            texts.Remove(data);
            dirty = true;
        }
        public void ClearText()
        {
            texts.Clear();
            dirty = true;
        }
        
        public TextData AddText(string text, Color color, int posX, int posY)
        {
            TextData data = new TextData(text, posX, posY, color, this);
            data.Text = text;
            data.ARGB = color;
            data.PositionX = posX;
            data.PositionY = posY;
            
            dirty = true;
            texts.Add(data);
            return data;            
        }
        
        public void DrawTexts()
        {
            if(dirty)
            {
                //img = new Image(ImageMode.Rgba, new ImageSize(width, height), new ImageColor(0, 0, 0, 255));            
                foreach(TextData txt in texts)
                {
                    DrawText(txt.Text.ToCharArray(), txt.ARGB, txt.PositionX, txt.PositionY);
                }
                
                texture.SetPixels(0, img.ToBuffer(), 0, 0 );
                //img.Dispose();
                
                dirty = false;
            }
        }
        private void DrawText(char[] text, Color color, int posX, int posY)
        {
            int curX = posX;
            int curY = posY;
            
            foreach(char c in text)
            {
                // get the letter from the font and draw
                Image letterImage = CSimpleFont.Instance.GetLetter(c);
                img.DrawImage(letterImage, new ImagePosition(curX, curY));
                
                // move on
                curX += CSimpleFont.Instance.CharWidth;
            }
            
        }
    }
}

